package eval.cm.xml.parser.simple.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class TstUtility {
	
	/**
	 * Produces the {@link URL} of a resource inside classpath
	 * @param location The classpath
	 * @return The URL of the resource
	 */
	public static URL findClasspathResource( final String location ) {
		ClassLoader cLoader = Thread.currentThread().getContextClassLoader();
		return cLoader.getResource(location);
	}
	
	/**
	 * Reads the content of a classpath resource if found
	 * @param location The classpath
	 * @return The content of the resource found at classpath (UTF-8)
	 */
	public static String getClasspathResourceContent(final String location) {
		final URL curl = findClasspathResource(location);
		try {
			final InputStream in = curl.openConnection()
				.getInputStream();
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			byte[] buff = new byte[1024];
			int len = -1;
			while( -1 != ( len = in.read( buff) ) ) {
				bout.write( buff, 0, len );
			}
			in.close();
			return new String(bout.toByteArray());
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static String removeWhitespaces(final String input) {
		return input.replaceAll("\\s+", "");
	}

}

